package com.fyl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.fyl.core.response.RS;
import com.fyl.domain.BaseModel;
import com.fyl.utils.PageQuery;
import com.fyl.utils.PageQueryWrappers;
import com.fyl.utils.PageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

public class BaseController<S extends IService<T>, T extends BaseModel> {

    @Autowired
    protected S baseService;

    public S getBaseService() {
        return baseService;
    }


    @GetMapping({"/page"})
    public RS<PageResp<T>> page(@ModelAttribute PageQuery pageQuery, @RequestParam Map<String, Object> params) {
        QueryWrapper<T> pqw = PageQueryWrappers.pageQuery(params);
        Page<T> page = PageQuery.buildPage(pageQuery);
        IPage<T> page1 = baseService.page(page, pqw);
        PageResp<T> pageResp = PageResp.build(page1);
        return RS.genSuccess(pageResp);
    }

    @GetMapping({"/get"})
    public RS<T> get(@RequestParam Long id) {
        T t = baseService.getById(id);
        return RS.genSuccess(t);
    }


}
